package sfin.service;

import static org.apache.commons.lang.StringUtils.isBlank;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;

import sfin.swt.exception.StatizoException;
import sfin.swt.util.FormatterUtil;
import br.com.statizo.financeiro.core.repository.impl.CartaoRepository;
import br.com.statizo.financeiro.domain.entity.impl.CartaoCredito;
import br.com.statizo.financeiro.domain.entity.impl.ContaCorrente;

public class CartaoService extends AbstractService{

	private List<CartaoCredito> cartoes;

	private ContaService contaService;
	private CartaoRepository cartaoRepository;

	public CartaoService(ResourceBundle bundle, FormatterUtil formatterUtil, CartaoRepository cartaoRepository) {
		super(bundle,formatterUtil);
		this.cartaoRepository = cartaoRepository;
		cartoes = cartaoRepository.getList();
		sort();
	}

	public int save(int index, String nome, int conta) throws StatizoException{

		List<String> errors = new ArrayList<String>();

		if(isBlank(nome)){
			errors.add(bundle.getString("credit.card.edit.validation.required.name"));
		}

		if(conta < 0){
			errors.add(bundle.getString("credit.card.edit.validation.required.current-account"));			
		}

		if(errors.isEmpty()){

			CartaoCredito cartao = index >= 0 ? cartoes.get(index) : new CartaoCredito();
			cartao.setNome(nome);
			ContaCorrente cc = contaService.getAt(conta);
			cartao.setConta(cc);

			try{
				
				cartaoRepository.entityManagerRefresh();
				cartaoRepository.beginTransaction();
				cartao = cartaoRepository.merge(cartao);
				cartaoRepository.commit();

				if(!cartoes.contains(cartao)){
					cartoes.add(cartao);
				}

				sort();
				notifyListeners();

			}catch(Exception e){
				errors.add(e.getMessage());
				throw new StatizoException(errors);
			}
			return cartoes.indexOf(cartao);
		}else{
			throw new StatizoException(errors);
		}

	}


	public String[][] getAll(){
		String[][] all = new String[cartoes.size()][2];
		for(int i = 0; i<cartoes.size(); i++){
			CartaoCredito cartao = cartoes.get(i);
			all[i][0] = cartao.getNome();
			all[i][1] = cartao.getConta().getApelido();
			
		}
		return all;
	}
	
	
	public void sort(){
		Comparator<CartaoCredito> comparator = (c1,c2) -> c1.getNome().compareTo(c2.getNome());
		cartoes.sort(comparator);
	}

	public String[] getNomes(){
		String[] nomes = new String[cartoes.size()];
		for (int i = 0; i < nomes.length; i++) {
			nomes[i] = cartoes.get(i).getNome();
		}
		return nomes;
	}

	public int indexOf(CartaoCredito cartao){
		return cartoes.indexOf(cartao);
	}

	public CartaoCredito getAt(int index){
		return cartoes.get(index);
	}

	public void setContaService(ContaService contaService) {
		this.contaService = contaService;
	}

	public void delete(int selectedIndex) throws StatizoException {
		List<String> errors = new ArrayList<String>();
		try{
			cartaoRepository.entityManagerRefresh();
			cartaoRepository.beginTransaction();
			cartaoRepository.remove(cartoes.get(selectedIndex).getId());
			cartaoRepository.commit();
			cartoes.remove(selectedIndex);
			notifyListeners();
		}catch(Exception e){
			errors.add(e.getMessage());
			throw new StatizoException(errors);
		}
	}

	public int count() {
		return cartoes.size();
	}

}